package com.fxiao.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fxiao.dao.UserDao;
import com.fxiao.entity.T_User;
import com.fxiao.util.StringHelper;
import com.mysql.jdbc.StringUtils;

public class UserController extends HttpServlet {

	private static final long serialVersionUID = 3136876985484118493L;

	private static final UserDao userDao = new UserDao();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String n = req.getParameter("n");

		if (n != null && n.equals("loginUser")) {
			// 登录
			System.out.println("进入用户登录页面了。。。");
			login(req, resp);
		} else if (n != null && n.equals("addUser")) {
			// 添加页面
			System.out.println("进入用户添加页面了。。。");
			addUser(req, resp);
		} else if (n != null && n.equals("queryUser")) {
			// 修改个人资料前查询
			System.out.println("修改个人资料前查询了。。。");
			queryUser(req, resp);
		} else if (n != null && n.equals("saveUser")) {
			// 修改个人资料保存
			System.out.println("进入修改个人资料保存了。。。");
			saveUser(req, resp);
		} else if (n != null && n.equals("exit")) {
			System.out.println("退出了。。。");
			req.getSession().removeAttribute("user");
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
		}

	}

	/**
	 * 用户登录
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings("unused")
	protected void login(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();

		System.out.println("进入登录login！！！！");

		// 获取用户
		String user_id = req.getParameter("username");
		// 获取密码
		String user_pass = req.getParameter("password");

		// 返回结果
		String result = userDao.login(user_id, StringHelper.MD5(user_pass));

		if (result.equals("3")) {
			// 用户名密码都正确
			// 把用户名存入到session中
			session.setAttribute("user", user_id);
			session.setMaxInactiveInterval(3600);
			resp.sendRedirect(req.getContextPath() + "/sysadmin/index/index.jsp");
			System.out.println(req.getContextPath());
		} else {
			req.setAttribute("msg", "用户名或者密码错误！");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}

	}

	/**
	 * 添加用户
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void addUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		System.out.println("进入添加用户addUser！！！！");

		String user_id = req.getParameter("user_id");
		String user_pass = req.getParameter("user_pass");
		System.out.println("登录帐号=" + user_id + ";登录密码=" + user_pass);
		String creator = req.getParameter("creator");
		String contactor = req.getParameter("contactor");
		System.out.println("推荐人=" + creator + ";接点人=" + contactor);
		String user_level = req.getParameter("user_level");
		String CPbalance = req.getParameter("CPbalance");
		String CPmoney = req.getParameter("CPmoney");
		System.out.println("会员星级=" + user_level + ";CP余额=" + CPbalance + ";所需CP=" + CPmoney);
		String location = req.getParameter("location");
		String user_email = req.getParameter("user_email");
		String user_name = req.getParameter("user_name");
		System.out.println("注册位置=" + location + ";邮箱=" + user_email + ";姓名=" + user_name);
		String user_code = req.getParameter("user_code");
		String user_sex = req.getParameter("user_sex");
		String user_country = req.getParameter("user_country");
		System.out.println("身份证号=" + user_code + ";性别=" + user_sex + ";国家=" + user_country);
		String user_phone = req.getParameter("user_phone");
		String user_bank = req.getParameter("user_bank");
		String bank_name = req.getParameter("bank_name");
		System.out.println("手机=" + user_phone + ";银行=" + user_bank + ";银行卡姓名=" + bank_name);
		String bank_account = req.getParameter("bank_account");
		String user_alipay = req.getParameter("user_alipay");
		String safe_pass = req.getParameter("safe_pass");
		System.out.println("银行卡号=" + bank_account + ";支付宝帐号=" + user_alipay + ";支付安全密码=" + safe_pass);

		T_User user = userDao.getUserByUserId(user_id);
		PrintWriter out = resp.getWriter();
		if (user == null) {
			if (!StringUtils.isNullOrEmpty(user_pass)) {
				// 表示可以使用
				user = new T_User();
				user.setUser_id(user_id);
				user.setUser_pass(StringHelper.MD5(user_pass));
				user.setCreator(creator);
				user.setContactor(contactor);
				user.setUser_level(Integer.parseInt(user_level));
//				user.setCPbalance(Float.parseFloat(CPbalance));
//				user.setCPmoney(Float.parseFloat(CPmoney));
				user.setLocation(Integer.parseInt(location));
				user.setUser_email(user_email);
				user.setUser_name(user_name);
//				user.setUser_code(user_code);
//				user.setUser_sex(Integer.parseInt(user_sex));
//				user.setUser_country(Integer.parseInt(user_country));
//				user.setUser_phone(user_phone);
//				user.setUser_bank(Integer.parseInt(user_bank));
//				user.setBank_name(bank_name);
//				user.setBank_account(bank_account);
//				user.setUser_alipay(user_alipay);
//				user.setSafe_pass(StringHelper.MD5(safe_pass));
				userDao.add(user);
				req.setAttribute("msg", "添加成功！");
				req.getRequestDispatcher("addUser.jsp").forward(req, resp);
			} else {
				out.println("0");
			}

		} else {
			// 表示不可使用
			if (!StringUtils.isNullOrEmpty(user_pass)) {
				req.setAttribute("msg", "帐号已经被添加了，请更换！");
			} else {
				out.println("1");
			}
		}
	}

	/**
	 * 修改前的查询方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void queryUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("进入个人资料修改前查询queryUser！！！");
		HttpSession session = req.getSession();
		String user_id = session.getAttribute("user").toString();
		System.out.println("user_id=" + user_id);
		T_User user = userDao.getUserByUserId(user_id);
		req.setAttribute("user", user);
		req.getRequestDispatcher("/sysadmin/shop/modify.jsp").forward(req, resp);
	}

	/**
	 * 修改的保存方法
	 */
	protected void saveUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("进入个人资料修改保存saveUser！！！");

		HttpSession session = req.getSession();
		// 调用业务逻辑处理类
		T_User user = userDao.getUserByUserId(session.getAttribute("user").toString());
		String user_id = session.getAttribute("user").toString();
		System.out.println("user_id=" + user_id);
		// 修改密码，不填不修改
		String user_pass = req.getParameter("user_pass");
		if (user_pass != "") {
			// 设置新密码
			user.setUser_pass(StringHelper.MD5(user_pass));
		} else {
			// 保持原来的密码
			user.setUser_pass(req.getParameter("hf_user_pass"));
		}
		// user.setCreator(req.getParameter("creator"));
		// user.setContactor(req.getParameter("contactor"));
		// user.setUser_level(Integer.parseInt(req.getParameter("user_level")));
		user.setCPbalance(Float.parseFloat(req.getParameter("CPbalance")));
		user.setCPmoney(Float.parseFloat(req.getParameter("CPmoney")));
		// user.setLocation(Integer.parseInt(req.getParameter("location")));
		user.setUser_email(req.getParameter("user_email"));
		user.setUser_name(req.getParameter("user_name"));
		user.setUser_code(req.getParameter("user_code"));
		user.setUser_sex(Integer.parseInt(req.getParameter("user_sex")));
		user.setUser_country(Integer.parseInt(req.getParameter("user_country")));
		user.setUser_phone(req.getParameter("user_phone"));
		user.setUser_bank(Integer.parseInt(req.getParameter("user_bank")));
		user.setBank_name(req.getParameter("bank_name"));
		user.setBank_account(req.getParameter("bank_account"));
		user.setUser_alipay(req.getParameter("user_alipay"));
		user.setSafe_pass(StringHelper.MD5(req.getParameter("safe_pass")));
		// 修改支付安全密码，不填不修改
		String safe_pass = req.getParameter("safe_pass");
		if (safe_pass != "") {
			// 设置新密码
			user.setSafe_pass(StringHelper.MD5(safe_pass));
		} else {
			// 保持原来的密码
			user.setSafe_pass(req.getParameter("hf_safe_pass"));
		}

		boolean b = userDao.saveUserInfo(user);
		if (b) {
			req.setAttribute("msg", "恭喜！修改信息成功！");
		} else {
			req.setAttribute("msg", "对不起！修改信息失败！");
		}
		req.setAttribute("user", user);
		req.getRequestDispatcher("/sysadmin/shop/modify.jsp").forward(req, resp);

	}

}
